package com.hzhr.iot.user.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.user.sdk.constant.User_ErrorsEnum;
import com.hzhr.iot.user.sdk.model.dept_parentId.Dept_ParentIdGetModel;
import com.hzhr.iot.user.sdk.model.dept_parentId.Dept_ParentIdModel;
import com.hzhr.iot.user.sdk.model.dept_parentId.Dept_ParentIdPutModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class User_Dept_ParentIdService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 管理员更新部门的上级编号 <br/><br/>
     * 简介&nbsp;:&nbsp;更新部门的上级部门编号 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据路径参数deptId查询登录人的管辖范围下的部门(DEPT)dept_1，如果dept_1不存在，抛出异常201； <br/>
     * 2.&nbsp;查询上级部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body.parentId查询登录人的管辖范围下的部门(DEPT)dept_2，如果dept_2不存在，抛出异常202； <br/>
     * 3.&nbsp;验证部门是否具备更新上级部门编号的条件&nbsp;:&nbsp;(√) <br/>
     * &nbsp;条件1：dept_1.name在dept_2的直接下级部门中不能存在，如果存在，抛出异常203； <br/>
     * &nbsp;条件2：dept_1不能存在下级部门或下级用户，如果存在，抛出异常204； <br/>
     * 4.&nbsp;更新部门信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body设置dept_1的相应属性，并且同时设置以下属性： <br/>
     * &nbsp;(1)&nbsp;dept_1.parentName&nbsp;=&nbsp;dept_2.name； <br/>
     * &nbsp;(2)&nbsp;dept_1.code的设置规则： <br/>
     * &nbsp;如果dept_2不存在直接下级部门，则设置为&nbsp;dept_2.code&nbsp;+&nbsp;'01'； <br/>
     * &nbsp;如果dept_2存在直接下级部门，则查询出其直接下级部门的最大code，提取出该code的最后两位，转换为数字并在此基础上&nbsp;+1&nbsp;， <br/>
     * &nbsp;然后左补零至两位字符串str_1，最后将dept_1.code设置为&nbsp;dept_2.code&nbsp;+&nbsp;str_1； <br/>
     * &nbsp;(3)&nbsp;dept_1.resourceCode&nbsp;=&nbsp;dept_2.code&nbsp;+&nbsp;“dept_1.code(上一步生成)的最后两位”； <br/>
     * 5.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询更新结果，组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param deptId 部门编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see Dept_ParentIdPutModel body <br/>
     * @see Dept_ParentIdGetModel result <br/>
     */ 
    public Dept_ParentIdGetModel putByDeptId(Map<String, String> header, Integer deptId, Map<String, Object> query, Dept_ParentIdPutModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("deptId", deptId);

        try {
            ResponseEntity<Dept_ParentIdGetModel> result = restTemplate.exchange("http://user-service/depts/{deptId}/dept-ids?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.PUT, httpEntity, Dept_ParentIdGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPT_PARENTIDSERVICE_PUT_BY_DEPT_ID_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(User_ErrorsEnum.getEnumByName("DEPT_PARENTIDSERVICE_PUT_BY_DEPT_ID_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

